package com.abc.cloud.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.abc.cloud.common.utils.UserHolder;
import com.abc.cloud.dto.im.ChatResp;
import com.abc.cloud.vo.im.AdminUpdateChatReq;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.abc.cloud.entity.ChatEntity;
import com.abc.cloud.service.ChatService;
import com.abc.cloud.common.utils.PageUtil;
import com.abc.cloud.common.utils.Result;



/**
 * 聊天表
 *
 * @author LiJunXi
 * @email 2770063826@qq.com
 * @date 2023-11-29 13:17:40
 */
@Api(tags = "聊天接口")
@RestController
@RequestMapping("/chat")
public class ChatController {
    @Autowired
    private ChatService chatService;

    /**
     * 列表
     */
    @ApiOperation("获取聊天列表")
    @GetMapping("/list")
    public Result<List<ChatResp>> list(){
        Long userId = UserHolder.getUser().getId();
        List<ChatResp> chatEntityList = chatService.listByUserId(userId);

        return Result.success(chatEntityList, "获取成功");
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public Result info(@PathVariable("id") Long id){
		ChatEntity chat = chatService.getById(id);

        return Result.success(chat, "chat");
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public Result save(@RequestBody ChatEntity chat){
		chatService.save(chat);

        return Result.success(null, "");
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public Result update(@RequestBody ChatEntity chat){
		chatService.updateById(chat);

        return Result.success(null, "");
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public Result delete(@RequestBody Long[] ids){
		chatService.removeByIds(Arrays.asList(ids));

        return Result.success(null, "");
    }

    @ApiOperation("管理员分页获取聊天列表")
    @GetMapping("/admin/list")
    public Result adminChatList(@RequestParam Map<String, Object> params){
        PageUtil page = chatService.adminChatList(params);

        return Result.success(page, "获取成功");
    }

    @ApiOperation("管理员修改聊天信息")
    @PutMapping("/admin/update")
    public Result adminUpdateChat(@RequestBody AdminUpdateChatReq adminUpdateChatReq){
        chatService.adminUpdateChat(adminUpdateChatReq);

        return Result.success(null, "操作成功");
    }

    @ApiOperation("管理员批量修改聊天状态")
    @PutMapping("/admin/updateStatus/{status}")
    public Result adminDeleteChat(@RequestBody List<Long> ids,
                                  @PathVariable("status") Integer status){
        chatService.adminDeleteChat(ids, status);

        return Result.success(null, "操作成功");
    }
}
